`
https://leetcode.cn/problems/minimum-distance-between-bst-nodes/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var minDiffInBST = function (root) {
  let res = Infinity
  let lastNode = null
  const traverse = (root) => {
    if (root === null) return

    traverse(root.left)

    if (lastNode) {
      res = Math.min(res, Math.abs(lastNode.val - root.val))
    }
    lastNode = root

    traverse(root.right)
  }

  traverse(root)

  return res
};